﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DoubleClick.Runner;
using DoubleClick.Logging;

namespace DoubleClick.Windows
{
    public class Server
    {
        private IConfiguration configuration;
        private RunCommand rc;
        DoubleClick.Logging.ILogger log =
            new DoubleClick.Logging.EventLogger(
               Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\DoubleClick.log");

        public Server(IConfiguration c)
        {
            if (!File.Exists(c.CommandToRun))
                throw new FileNotFoundException();

            configuration = c;
        }

        public void Start()
        {
            if (Program.Default.StartupMode == StartupModeEnum.Interactive)
                log.Write(EventLogType.information,
                    Environment.GetCommandLineArgs()[0] + ": Il controller è stato avviato per il processo " +
                        Program.Default.CommandToRun + ".");

            rc = new Runner.RunCommand(configuration.CommandToRun);
            rc.SetLogger(log);
            rc.Run();
        }

        public void Stop()
        {
            rc.Dispose();
        }
    }
}
